package com.bill99.golden.inf.sso.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bill99.golden.inf.sso.util.CM;
import com.bill99.golden.info.sso.model.Token;

public class TokenDao extends BaseDao {
	public void saveToken(Token token) {
		if (token == null) {
			throw new RuntimeException("token cannot be null!!!!!!!!!!!");
		}
		token.setId(this.getSsoSeqId());
		this.dao.insert("token.insertToken", token);
	}

	public void destryToken(String token) {
		if (token == null) {
			throw new RuntimeException("token cannot be null!!!!!!!!!!!");
		}
		Map m = new HashMap();
		m.put("state", new Long(0));
		m.put("destroyTime", new Date());
		m.put("token", token);
		dao.update("token.updateToken", m);
	}

	public void validateToken(String token, Long visitCnt, Date lastAccessTime) {
		if (token == null) {
			throw new RuntimeException("token cannot be null!!!!!!!!!!!");
		}
		Map m = new HashMap();
		m.put("validateCount", visitCnt);
		m.put("lastAccessTime", lastAccessTime);
		m.put("token", token);
		dao.update("token.updateToken", m);
	}

	public void deleteInvalidToken(int reserveTokenDays) {
		Map m = new HashMap();
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, -reserveTokenDays);
		m.put("date", CM.formatDate(cal, "yyyyMMdd"));
		dao.delete("token.deleteInvalidToken", m);
	}

	public List getTokenListFromDB() {
		Map m = new HashMap();
		m.put("state", new Long(1));
		List list = dao.queryForList("token.queryToken", m);
		return list;
	}
}
